import easyquotation
import baostock as bs

from util.daylink import daylink
from scipy.signal import find_peaks
import numpy as np
import pandas as pd
from scipy import signal   #滤波等
import matplotlib.pyplot as plt




lg = bs.login(user_id='anonymous', password='123456')
fromdate = daylink().get_day_of_day(-100)
todate = daylink().get_day_of_day(0)
#lg = bs.login(user_id='anonymous', password='123456') ,tradestatus
field = 'date, code, open, high, low, close,tradestatus'
rs = bs.query_history_k_data_plus('sz.000524',
                                              field,
                                              # "date,code,open,high,low,close,preclose,volume,amount,adjustflag,turn,tradestatus,pctChg,peTTM,pbMRQ,psTTM,pcfNcfTTM,isST",
                                              start_date='%s-%s-%s' % (
                    fromdate.year, fromdate.month, fromdate.day), end_date='%s-%s-%s' % (
                    todate.year, todate.month, todate.day),
                                              frequency='d',
                                              adjustflag="2")

result_list = []
#print(rs.get_row_data())
while (rs.error_code == '0') & rs.next():

    d = rs.get_row_data()
    result_list.append(d)

df_init = pd.DataFrame(result_list, columns=rs.fields)
#print(df_init)


df_init = df_init.astype({'high': 'float', 'open': 'float', 'close': 'float', 'low': 'float'})
df_init.date = pd.to_datetime(df_init.date)
value_list = df_init.high.tolist()
value_list = np.array(value_list)
time_list = df_init.date.tolist()
time_list=np.array(time_list)

indices = find_peaks(value_list, height=None, threshold=None, distance=None,
                             prominence=None, width=None, wlen=None, rel_height=None,
                             plateau_size=None)
stockdata = pd.DataFrame(columns=['val', 'ind', 'type','time'])
#print(indices)

val = value_list[indices[0]]
ind = indices[0]
plt.figure(figsize=(6, 4))
plt.plot(np.arange(len(value_list)), value_list)
plt.plot(indices[0], value_list[indices[0]], 'o')
for i in range(0, len(ind)):
       stockdata = stockdata.append({'val': val[i], 'ind': ind[i], 'type': 1,'time':time_list[ind[i]]}, ignore_index=True)


value_list =df_init.low.tolist()
value_list = np.array(value_list)
#print(value_list)
value_list = value_list*-1
indices = find_peaks(value_list, height=None, threshold=None, distance=None,
                             prominence=None, width=None, wlen=None, rel_height=None,
                             plateau_size=None)
#stockdata = pd.DataFrame(columns=['val', 'ind', 'type'])
val = value_list[indices[0]]
ind = indices[0]
value_l = value_list * -1

plt.plot(np.arange(len(value_list)), value_list*-1)
plt.plot(indices[0], value_l[indices[0]], 'o')
plt.show()
for i in range(0, len(ind)):
            print(ind[i],val[i])
    #stockdata = stockdata.append({'val': val[i]*-1, 'ind': ind[i], 'type': 2,'time':time_list[ind[i]]}, ignore_index=True)
stockdata = stockdata.sort_values(by=['ind'], ascending=False)

